package com.flow.manager.action;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

import com.flow.model.PagerModel;
import com.flow.model.RolePermission;
import com.flow.model.RoleTab;
import com.flow.service.IRolePermissionService;
import com.flow.service.IRoleTabService;
import com.flow.tools.StringTools;

/**
 * 角色权限控制器
 * 
 * @author wang
 * @2016-1-7 RolePermissionAction.java
 * 
 */
public class RolePermissionAction {

	/**
	 * 服务引用
	 */
	private IRolePermissionService<RolePermission> rpService;

	/**
	 * 选中的
	 */
	private List<Integer> checkedStatus = new ArrayList<Integer>();

	/**
	 * 服务层引用
	 */
	private IRoleTabService<RoleTab> rtService;

	/**
	 * 角色ID
	 */
	private String rtId;
	
	/**
	 * 设置角色权限菜单 
	 */
	public String setRolePermissionInfo(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("menuId");
		String rtId = request.getParameter("rtId");

		try {

			if(rpService.setRolePermissionInfo(rtId, checkedStatus, id))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
		
	}

	/**
	 * 设置角色增删改查权限
	 */
	public String setPermissionInfo() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String rpId = request.getParameter("rpId");
		String insert = request.getParameter("rpInsert");
		String delete = request.getParameter("rpDelete");
		String update = request.getParameter("rpUpdate");
		String find = request.getParameter("rpFind");
		rtId = request.getParameter("rtId");
		try {

			rpService.setPermissionInfo(rpId, insert, delete, update, find);
			return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 加载角色权限
	 */
	public String loadRolePermissionInfo() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			String rtId = request.getParameter("rtId");
			RoleTab roleTab = this.rtService.findRoleTabById(rtId);
			List<RolePermission> rpList = this.rpService
					.findRolePermissionByRtId(rtId);
			String menuId = "";
			if (rpList != null) {
				for (RolePermission rolePermission : rpList) {
					menuId = menuId
							+ rolePermission.getPermissionMenu().getPmId()
							+ ",";
				}
				request.setAttribute("menuId", menuId);
				request.setAttribute("rpList", rpList);
			}
			request.setAttribute("roleTab", roleTab);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}
		return "ERROR";
	}

	/**
	 * 分页列表 
	 */
	public String findRolePermissionByPager() {
		
		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			String id = request.getParameter("rtId");
			id = (!StringTools.nullOrEmpty(id)) ? "0" : id;
			PagerModel pager = this.rpService.findRolePermissionByPager(id);
			request.setAttribute("pager", pager);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 角色权限列表 
	 */
	public String findRolePermission() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			String id = request.getParameter("rtId");
			id = (!StringTools.nullOrEmpty(id))  ? "0" : id;
			List<RolePermission> list = this.rpService
					.findRolePermissionByRtId(id);
			request.setAttribute("list", list);

			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 根据ID查询 
	 */
	public String findRolePermissionById() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			String id = request.getParameter("rpId");
			id = (!StringTools.nullOrEmpty(id)) ? "0" : id;
			RolePermission rp = this.rpService.findRolePermissionById(id);
			request.setAttribute("rp", rp);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	public List<Integer> getCheckedStatus() {
		return checkedStatus;
	}

	public void setCheckedStatus(List<Integer> checkedStatus) {
		this.checkedStatus = checkedStatus;
	}

	public void setRpService(IRolePermissionService<RolePermission> rpService) {
		this.rpService = rpService;
	}

	public void setRtService(IRoleTabService<RoleTab> rtService) {
		this.rtService = rtService;
	}
	
	public String getRtId() {
		return rtId;
	}

	public void setRtId(String rtId) {
		this.rtId = rtId;
	}

}
